package com.ruoyi.manager.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.manager.domain.Member;
import com.ruoyi.manager.domain.Place;
import com.ruoyi.manager.domain.ReservationDto;
import com.ruoyi.manager.mapper.PlaceMapper;
import com.ruoyi.manager.service.IMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.ruoyi.manager.mapper.TReservationMapper;
import com.ruoyi.manager.domain.TReservation;
import com.ruoyi.manager.service.ITReservationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@Service
public class TReservationServiceImpl implements ITReservationService 
{
    @Autowired
    private TReservationMapper tReservationMapper;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private PlaceMapper placeMapper;
    /**
     * 查询预约管理
     * 
     * @param reservationId 预约管理主键
     * @return 预约管理
     */
    @Override
    @Cacheable(cacheNames = "reservationCache" , key = "#reservationId")
    public TReservation selectTReservationById(Long reservationId)
    {
        return tReservationMapper.selectTReservationById(reservationId);
    }

    /**
     * 查询预约管理列表
     * 
     * @param tReservation 预约管理
     * @return 预约管理
     */
    @Override
    public List<TReservation> selectTReservationList(TReservation tReservation)
    {
        return tReservationMapper.selectTReservationList(tReservation);
    }

    /**
     * 新增预约管理
     * 
     * @param tReservation 预约管理
     * @return 结果
     */
    @Override
    public int insertTReservation(TReservation tReservation)
    {
        tReservation.setCreateTime(DateUtils.getNowDate());
        return tReservationMapper.insertTReservation(tReservation);
    }

    /**
     * 修改预约管理
     * 
     * @param tReservation 预约管理
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "reservationCache" , key = "#tReservation.id")
    public int updateTReservation(TReservation tReservation)
    {
        return tReservationMapper.updateTReservation(tReservation);
    }

    /**
     * 批量删除预约管理
     * 
     * @param ids 需要删除的预约管理主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "reservationCache" , allEntries = true)
    public int deleteTReservationByIds(Long[] ids)
    {
        return tReservationMapper.deleteTReservationByIds(ids);
    }

    /**
     * 删除预约管理信息
     * 
     * @param id 预约管理主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "reservationCache" , allEntries = true)
    public int deleteTReservationById(Long id)
    {
        return tReservationMapper.deleteTReservationById(id);
    }


    /**
     * 检查时间段是否被占用
     * @param start
     * @param end
     * @return
     */
    @Override
    public boolean checkTimes(Long placeId , LocalDateTime start, LocalDateTime end) {
        //查询该场地所有预约  除去 已完成 和 已取消
        List<TReservation> reservations = tReservationMapper.selectByPlaceId(placeId);
        if (reservations == null || reservations.isEmpty()){
            return true;
        }
        for (TReservation reservation : reservations) {
            //计算每条预约的时间段，与传入的时间段对比
            LocalDateTime startTime = reservation.getStartTime();
            LocalDateTime endTime = startTime.plusMinutes(reservation.getDuration());
            //如果有任意一个时间段相交 则返回false
            if (start.isBefore(endTime) && end.isAfter(startTime)){
                return false;
            }
        }
        return true;
    }


    /**
     * 插入用户预约
     * @param reservationDto
     * @return
     */
    @Transactional
    @Override
    public int insertUserTReservation(ReservationDto reservationDto){

        //根据姓名和手机号查询用户
        Member member = memberService.selectByNameAndPhone(reservationDto.getMemberName(),reservationDto.getPhone());

        if(member == null){
            return -1;
        }

        //查询场馆状态 如果为维护 2  和 闭馆 3 不予预约
        Place place = placeMapper.selectPlaceById(reservationDto.getCourtId());
        if (place.getStatus() == 2 || place.getStatus() == 3){
            throw new RuntimeException("场馆维护或闭馆");
        }

        //计算用户扣款信息
        Long actualFee = member.getBalance() - reservationDto.getActualFee();
        if (actualFee < 0){
            throw new RuntimeException("余额不足");
        }

        member.setBalance(actualFee);
        memberService.updateMember(member);

        //设置预约信息
        TReservation tReservation = new TReservation();
        BeanUtils.copyProperties(reservationDto , tReservation);
        tReservation.setMemberId(member.getId());


        tReservation.setCreateTime(DateUtils.getNowDate());

        return tReservationMapper.insertTReservation(tReservation);
    }
}
